#include "base/base_buffer.h"
#include "net/buffer.h"

#include "net/buffer_reader.h"
namespace net_tools::net
{
  Buffer_reader::Buffer_reader()
      : cur_buffer_(nullptr){};

  void Buffer_reader::Init(Buffer *cur_buffer)
  {
    cur_buffer_ = cur_buffer;
    buffer_deque_.clear();
    len_ = cur_buffer_->residue_len_;
    unsigned int temp_len = len_;
    net_tools::base::Base_buffer *begin = cur_buffer_->head_recv_buf_;
    while (temp_len)
    {
      temp_len -= begin->Get_read_residue();
      buffer_deque_.push_back(
          std::tuple<net_tools::base::Base_buffer *, unsigned int>(begin, begin->Get_read_residue()));
      begin = begin->Get_next();
    }
  };

  void Buffer_reader::Read_all()
  {
    if (!cur_buffer_)
    {
      return;
    }
    while (cur_buffer_->residue_len_ > 0)
    {
      uint32_t now_write = cur_buffer_->residue_len_ > cur_buffer_->head_recv_buf_->Get_read_residue() ? cur_buffer_->head_recv_buf_->Get_read_residue() : cur_buffer_->residue_len_;
      cur_buffer_->head_recv_buf_->Read_append(now_write);
      cur_buffer_->residue_len_ -= now_write;
      cur_buffer_->Read_buf_clear();
    }
  };

  char *Buffer_reader::operator[](unsigned int index)
  {
    return nullptr;
  };

} // namespace net_tools::net
